Ext.define('Admin.view.overtime.OvertimeViewController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.overtimeViewController',

    OvertimeOpenApplyWindow: function(bt) {
        var json = Ext.util.JSON.decode(userInfo);
        var cfg = Ext.apply({
            xtype: 'overtimeViewWindow',
            items: [{
                xtype: 'overtimeApplyForm'
            }]
        }, {
            title: '加班申请表'
        });
        Ext.create(cfg);
        Ext.getCmp('overtimeApplyName').setValue(fullName);
        Ext.getCmp('overtimeDepartment').setValue(json.dept);
    },

    OvertimeOpenEditWindow: function(bt) {
        var grid = bt.up('gridpanel');
        var selModel = grid.getSelectionModel();

        if (selModel.hasSelection()) {
            var record = selModel.getSelection()[0];
            Ext.Ajax.request({
                url:'overtime/isChangeable',
                method:'post',
                defaultHeaders: {
                    "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
                },
                params:{
                    processInstanceId:record.data.processInstanceId
                },
                success:function(response,options){
                    var json = Ext.util.JSON.decode(response.responseText)
                    if (!json.success) {
                        Ext.Msg.alert('提示', json.msg+',不能进行修改');
                    } else {
                        var overtimeGridWindow = Ext.create('widget.overtimeViewWindow', {
                            title: '修改加班申请表',
                            items: [{
                                xtype: 'overtimeApplyForm'
                            }]
                        });
                        overtimeGridWindow.down('form').getForm().loadRecord(record);
                        var begin = Ext.getCmp('begin');
                        Ext.getCmp('end').setMinValue(begin.value);
                    }
                }
            });
        } else {
            Ext.Msg.alert('提示', '请选择一条消息');
        }
    },

    OvertimeGriddelete: function(bt) {
        var grid = bt.up('gridpanel');
        var selModel = grid.getSelectionModel();

        if (selModel.hasSelection()) {
            var record = selModel.getSelection()[0];
            Ext.Ajax.request({
                url:'overtime/isChangeable',
                method:'post',
                defaultHeaders: {
                    "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
                },
                params:{
                    processInstanceId:record.data.processInstanceId
                },
                success:function(response,options){
                    var json = Ext.util.JSON.decode(response.responseText)
                    if (!json.success) {
                        Ext.Msg.alert('提示', json.msg+',不能进行删除');
                    } else {
                        Ext.Ajax.request({
                            url: 'overtime/delete',
                            method: 'post',
                            defaultHeaders: {
                                "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
                            },
                            params: {
                                id: record.data.id
                            },
                            success: function(response,options){
                                var json = Ext.util.JSON.decode(response.responseText);
                                if (json.success) {
                                    Ext.Msg.alert('操作成功',json.msg,function(){
                                        grid.getStore().reload();
                                    })
                                } else {
                                    Ext.Msg.alert('操作失败',json.msg);
                                }
                            }
                        });
                    }
                }
            });
        } else {
            Ext.Msg.alert('提示', '请选择一条消息');
        }
    },

    OvertimeFromSubmit: function(bt) {
        var overtimeApplyForm = bt.up('form').getForm();
        var win = bt.up('window');

        overtimeApplyForm.submit({
            waitTitle: '请稍后...',
            waitMsg: '正在保存信息',
            url: 'overtime/saveOrUpdateForm',
            method: 'post',
            params: {
                "_csrf": document.getElementsByTagName('meta')['_csrf'].content
            },
            success: function(form, action) {
                win.close();
                Ext.Msg.alert('提示', action.result.msg);
                Ext.getCmp('overtimeGrid').getStore().reload();
            },
            failure: function(form, action) {
                Ext.Msg.alert('提示', action.result.msg);
            }
        });
    },

    OvertimeGridWindowClose: function(bt) {
        var win = bt.up('window');
        if (win) {
            win.close();
        }
    },

    OvertimeGridApprove:function(bt){
        var grid = bt.up('gridpanel');
        var selModel = grid.getSelectionModel();

        if (selModel.hasSelection()) {
            var record = selModel.getSelection()[0];
            Ext.Ajax.request({
                url:'overtime/isChangeable',
                method:'post',
                defaultHeaders: {
                    "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
                },
                params:{
                    processInstanceId:record.data.processInstanceId
                },
                success:function(response,options){
                    var json = Ext.util.JSON.decode(response.responseText);
                    if (json.success) {
                        Ext.Ajax.request({
                            url:'overtime/starOvertimeFlow',
                            method:'post',
                            defaultHeaders: {
                                "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
                            },
                            params:{
                                overtimeId:record.data.id,
                                processInstanceId:record.data.processInstanceId
                            },
                            success:function(response,options){
                                var json = Ext.util.JSON.decode(response.responseText);
                                if (json.success) {
                                    Ext.Msg.alert('操作成功',json.msg);
                                    grid.getStore().reload();
                                } else {
                                    Ext.Msg.alert('操作失败',json.msg);
                                }
                            }
                        });
                    } else {
                        Ext.Msg.alert('操作失败',json.msg);
                    }
                }
            })
                        
        } 
        else {
            Ext.Msg.alert('提示','请选择一个申请表')
        }
    },

    OvertimeOpenReapplyWindow:function(view,recIndex,cellIndex,item,e,record)
    {
        var win = Ext.create('widget.overtimeViewWindow',{
            title:'调整申请',
            items:{xtype:'overtimeReapplyForm'}
        });
        var form = win.down('form').getForm().loadRecord(record);
        var begin = Ext.getCmp('begin');
        Ext.getCmp('end').setMinValue(begin.value);
    },
    OvertimeReapply:function(bt){
        var form = bt.up('form').getForm();
        var win = bt.up('window');
        var date = new Date();
        var applicationTime = date.getFullYear()+'/'+(date.getMonth()+1)+'/'+date.getDate()+'/'+' '+date.getHours()+':'+date.getMinutes()+':'+date.getSeconds();
        form.submit({
            waitTitle: '请稍后...',
            waitMsg: '正在保存信息',
            url: 'overtime/saveOrUpdateForm',
            method: 'post',
            params: {
                applicationTime:applicationTime,
                "_csrf": document.getElementsByTagName('meta')['_csrf'].content
            },
            success: function(form, action) {
                Ext.Msg.alert('提示', action.result.msg);
                Ext.Ajax.request({
                    url:'overtime/getLeader.json',
                    method:'get',
                    success:function(response){
                        var keys=['reApply','depManager','applyUserId'];
                        var values = [true,response.responseText,userId];
                        var types = ['B','S','L'];
                        Ext.Ajax.request({
                            url:'overtime/overtimeComplete',
                            method:'post',
                            defaultHeaders: {
                                "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
                            },
                            params:{
                                taskId:form.findField('taskId').getValue(),
                                id:form.findField('id').getValue(),
                                processInstanceId:form.findField('processInstanceId').getValue(),
                                taskDefinitionKey:form.findField('taskDefinitionKey').getValue(),
                                keys:keys,
                                values:values,
                                types:types
                            },
                            success:function(response,options){
                                var json = Ext.util.JSON.decode(response.responseText);
                                if (json.success) {
                                    Ext.Msg.alert('提示',json.msg);
                                    win.close();
                                    if (Ext.getCmp('overtimeGrid')!=null) {
                                        Ext.getCmp('overtimeGrid').getStore().reload();
                                    }
                                    if (Ext.getCmp('overtimeApproveGrid')!=null) {
                                        Ext.getCmp('overtimeApproveGrid').getStore().reload();
                                    }
                                    
                                } else {
                                    Ext.Msg.alert('提示',json.msg);
                                }
                            }
                        })
                    }
                })

                
                        
            },
            failure: function(form, action) {
                Ext.Msg.alert('提示', action.result.msg);
            }
        })
    },

    OvertimeReapplyCancel:function(bt){
        var form = bt.up('form').getForm();
        var win = bt.up('window');
        var keys=['reApply'];
        var values = [false];
        var types = ['B']
        Ext.Ajax.request({
            url: 'overtime/overtimeComplete',
            method: 'post',
            defaultHeaders: {
                "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
            },
            params:{
                taskId:form.findField('taskId').getValue(),
                overtimeId:form.findField('id').getValue(),
                keys:keys,
                values:values,
                types:types
            },
            success: function(response, options) {
                var json = Ext.util.JSON.decode(response.responseText);
                if (json.success) {
                    win.close();
                    Ext.Msg.alert('提示', json.msg);
                    Ext.getCmp('overtimeGrid').getStore().reload();
                } else {
                    Ext.Msg.alert('提示', json.msg);
                }
            }
        })
    },

    overtimeGridPanelSearch:function(){
        var serchCreateTime = Ext.util.Format.date(this.lookupReference('overtimeGridSerchByCreateTime').getValue(),'Y/m/d H:i:s');
        var store = Ext.getCmp('overtimeGrid').getStore();

        
        Ext.apply(store.proxy.extraParams,{
            createDate:''
        });
        
        if (serchCreateTime!='') {
            Ext.apply(store.proxy.extraParams,{
                createDate:serchCreateTime
            });
        }else{
            delete store.getProxy().extraParams.createDate;
        }

        store.load({
            start: 0,
            limit: 25,
            page: 1
        })
    },

    overtimeGridPanelclear:function(){
        this.lookupReference('overtimeGridSerchByCreateTime').setValue('');
    }
})